﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Windows.Forms;
using DLL.Entities.Event;
using DLL.Entities.Status;
using DLL.Entities.Config;

namespace DLL.Entities
{
    // Right to Left
    // 1st ..\ = debug
    // 2nd ..\ = bin
    // 3rd ..\ = MDP

    public class ReadXML
    {
        #region Declarations
        COMConfig CC_Obj = new COMConfig();
        RADConfig RC_Obj = new RADConfig();
        MISConfig MC_Obj = new MISConfig();

        RADStatus RS_Obj = new RADStatus();
        MISStatus MS_Obj = new MISStatus();

        RADEvent RE_Obj = new RADEvent();
        COMEvent CE_Obj = new COMEvent();
        MISEvent ME_Obj = new MISEvent();

        XmlReader reader = null;
        #endregion

        #region Read Configs
        public COMConfig ReadCOMConfig()
        {
            try
            {
                reader = new XmlTextReader(@"..\..\..\COM\XML\COM_Config.xml");
                XmlSerializer serializer = new XmlSerializer(typeof(COMConfig));
                CC_Obj = (COMConfig)serializer.Deserialize(reader);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                reader.Close();
            }

            return CC_Obj;
        }

        public RADConfig ReadRADConfig()
        {
            try
            {
                reader = new XmlTextReader(@"..\..\..\RAD\XML\RAD_Config.xml");
                XmlSerializer serializer = new XmlSerializer(typeof(RADConfig));
                RC_Obj = (RADConfig)serializer.Deserialize(reader);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                reader.Close();
            }

            return RC_Obj;
        }

        public MISConfig ReadMISConfig()
        {
            try
            {
                reader = new XmlTextReader(@"..\..\..\MIS\XML\MIS_Config.xml");
                XmlSerializer serializer = new XmlSerializer(typeof(MISConfig));
                MC_Obj = (MISConfig)serializer.Deserialize(reader);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                reader.Close();
            }

            return MC_Obj;
        }
        #endregion

        #region Read Statuses
        public RADStatus ReadRADStatus(string dir)
        {
            try
            {
                reader = new XmlTextReader(dir);
                XmlSerializer serializer = new XmlSerializer(typeof(RADStatus));
                RS_Obj = (RADStatus)serializer.Deserialize(reader);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                reader.Close();
            }

            return RS_Obj;
        }

        public MISStatus ReadMISStatus(string dir)
        {
            try
            {
                reader = new XmlTextReader(dir);
                XmlSerializer serializer = new XmlSerializer(typeof(MISStatus));
                MS_Obj = (MISStatus)serializer.Deserialize(reader);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                reader.Close();
            }

            return MS_Obj;
        }
        #endregion

        #region Read Events
        public RADEvent ReadRADEvent(string dir)
        {
            try
            {
                reader = new XmlTextReader(dir);
                XmlSerializer serializer = new XmlSerializer(typeof(RADEvent));
                RE_Obj = (RADEvent)serializer.Deserialize(reader);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                reader.Close();
            }

            return RE_Obj;
        }

        public COMEvent ReadCOMEvent(string dir)
        {
            try
            {
                reader = new XmlTextReader(dir);
                XmlSerializer serializer = new XmlSerializer(typeof(COMEvent));
                CE_Obj = (COMEvent)serializer.Deserialize(reader);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                reader.Close();
            }

            return CE_Obj;
        }

        public MISEvent ReadMISEvent(string dir)
        {
            try
            {
                reader = new XmlTextReader(dir);
                XmlSerializer serializer = new XmlSerializer(typeof(MISEvent));
                ME_Obj = (MISEvent)serializer.Deserialize(reader);
            }

            catch (XmlException xe)
            {
                MessageBox.Show(xe.Message, "XML Parse Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException ioe)
            {
                MessageBox.Show(ioe.InnerException.Message, "XML Serialization Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception e1)
            {
                MessageBox.Show(Convert.ToString(e1));
            }
            finally
            {
                reader.Close();
            }

            return ME_Obj;
        }
        #endregion
    }
}
